package com.un.ebs.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.power.PowerResourceSet;
import com.un.ebs.sys.domain.EntDept;
import com.un.ebs.sys.dto.EntDeptAddDto;
import com.un.ebs.sys.dto.EntDeptUpdateDto;
import com.un.ebs.sys.dto.ObjectParentAndIndexChangeDto;
import com.un.ebs.sys.mapper.EntDeptMapper;
import com.un.ebs.sys.service.EntDeptService;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@Service
public class EntDeptServiceImpl extends ServiceImpl<EntDeptMapper, EntDept> implements EntDeptService {

    @Override
    public void add(EntDeptAddDto req) {
        EntDept obj = new EntDept();
        obj.setEntId(req.getEntId());
        obj.setName(req.getName());
        obj.setIsDelete(false);
        obj.setSort(0);
        obj.insert();
    }

    @Override
    public void update(EntDeptUpdateDto req) {
        EntDept obj = getById(req.getId());
        obj.setEntId(req.getEntId());
        obj.setName(req.getName());
        obj.setSort(0);
        obj.updateById();
    }

    @Override
    public EntDept queryById(String id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public void delete(String id) {
        getBaseMapper().deleteById(id);
    }

    @Transactional
    @Override
    public void changeNode(List<ObjectParentAndIndexChangeDto> req) {
        for (ObjectParentAndIndexChangeDto item : req) {
            EntDept obj = getById(item.getId());
            obj.setSort(item.getIndex());
            getBaseMapper().updateById(obj);
        }
    }

    @Bean
    @Qualifier("dept")
    public PowerResourceSet queryDept() {
        return null;
    }

    @Override
    public List<EntDept> queryByEntId(String entId) {
        LambdaQueryWrapper<EntDept> lq = Wrappers.lambdaQuery();
        lq.eq(EntDept::getEntId, entId);
        lq.eq(EntDept::getIsDelete, false);
        lq.orderByAsc(EntDept::getSort);
        return baseMapper.selectList(lq);
    }
}